package cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.common.mapper.CommonMapper;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.entity.Equipment;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.enums.EquipmentExceptionEnum;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.mapper.EquipmentMapper;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.pojo.request.EquipmentRequest;
import cn.stylefeng.guns.modular.deviceLedgerManagement.deviceLedger.service.EquipmentService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import oracle.sql.DATE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 设备台账业务实现层
 *
 * @author PANL
 * @since 2023/12/04 09:18
 */
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    CommonMapper commonMapper;

    @Override
    public void add(EquipmentRequest equipmentRequest) {
        Equipment equipment = new Equipment();
        BeanUtil.copyProperties(equipmentRequest, equipment);
        this.save(equipment);
    }

    @Override
    public void del(EquipmentRequest equipmentRequest) {
        Equipment equipment = this.queryEquipment(equipmentRequest);
        this.removeById(equipment.getEqpCode());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(EquipmentRequest equipmentRequest) {
        this.removeByIds(equipmentRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(EquipmentRequest equipmentRequest) {
        Equipment equipment = this.queryEquipment(equipmentRequest);
        BeanUtil.copyProperties(equipmentRequest, equipment);
        this.updateById(equipment);
    }

    @Override
    public Equipment detail(EquipmentRequest equipmentRequest) {
        return this.queryEquipment(equipmentRequest);
    }

    @Override
    public PageResult<Equipment> findPage(EquipmentRequest equipmentRequest) {
        LambdaQueryWrapper<Equipment> wrapper = createWrapper(equipmentRequest);
        Page<Equipment> pageList = this.page(PageFactory.defaultPage(), wrapper);
        pageList.setRecords(filterList(pageList.getRecords()));
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<Equipment> findList(EquipmentRequest equipmentRequest) {
        LambdaQueryWrapper<Equipment> wrapper = this.createWrapper(equipmentRequest);
        return this.list(wrapper);
    }

    List<Equipment> filterList(List<Equipment> equipmentList) {
        List<Map> eqpTypeList = commonMapper.getEqpTypeList();
        List<Map> eqpStation = commonMapper.getEqpStation();
        for (Equipment equipment : equipmentList) {
            for (Map map : eqpTypeList) {
                if (map.get("EQP_CLASS_CODE").toString().equals(equipment.getEqpClassCode())) {
                    equipment.setEqpClassCode(map.get("EQP_CLASS_NAME").toString());
                }
            }
            if (equipment.getStationCode() != null) {
                for (Map map : eqpStation) {
                    if (map.get("STATION_CODE").toString().equals(equipment.getStationCode())) {
                        equipment.setStationCode(map.get("STATION_NAME").toString());
                    }
                }
            }
        }
        return equipmentList;
    }

  /*  String filterDate(String date) {
        return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6, 8);
    }*/

    /**
     * 获取信息
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    private Equipment queryEquipment(EquipmentRequest equipmentRequest) {
        Equipment equipment = this.getById(equipmentRequest.getEqpCode());
        if (ObjectUtil.isEmpty(equipment)) {
            throw new ServiceException(EquipmentExceptionEnum.EQUIPMENT_NOT_EXISTED);
        }
        return equipment;
    }

    /**
     * 创建查询wrapper
     *
     * @author PANL
     * @since 2023/12/04 09:18
     */
    private LambdaQueryWrapper<Equipment> createWrapper(EquipmentRequest equipmentRequest) {
        LambdaQueryWrapper<Equipment> queryWrapper = new LambdaQueryWrapper<>();

        String eqpCode = equipmentRequest.getEqpCode();
       /* BigDecimal orgCode = equipmentRequest.getOrgCode();
        String EQPNAME = equipmentRequest.getEQPNAME();
        String eqpDesc = equipmentRequest.getEqpDesc();
        String MODEL = equipmentRequest.getMODEL();
        String TYPE = equipmentRequest.getTYPE();
        String eqpClassCode = equipmentRequest.getEqpClassCode();
        String eqpCompany = equipmentRequest.getEqpCompany();
        String CONTACT = equipmentRequest.getCONTACT();
        String TELPHONE = equipmentRequest.getTELPHONE();
        String eqpStatus = equipmentRequest.getEqpStatus();
        String CNCSYSTEM = equipmentRequest.getCNCSYSTEM();
        String BUYDATE = equipmentRequest.getBUYDATE();
        String eqpNum = equipmentRequest.getEqpNum();
        String stationCode = equipmentRequest.getStationCode();*/

        queryWrapper.like(ObjectUtil.isNotEmpty(eqpCode), Equipment::getEqpCode, eqpCode);
      /*  queryWrapper.eq(ObjectUtil.isNotNull(orgCode), Equipment::getOrgCode, orgCode);
        queryWrapper.like(ObjectUtil.isNotEmpty(EQPNAME), Equipment::getEQPNAME, EQPNAME);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpDesc), Equipment::getEqpDesc, eqpDesc);
        queryWrapper.like(ObjectUtil.isNotEmpty(MODEL), Equipment::getMODEL, MODEL);
        queryWrapper.like(ObjectUtil.isNotEmpty(TYPE), Equipment::getTYPE, TYPE);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpClassCode), Equipment::getEqpClassCode, eqpClassCode);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpCompany), Equipment::getEqpCompany, eqpCompany);
        queryWrapper.like(ObjectUtil.isNotEmpty(CONTACT), Equipment::getCONTACT, CONTACT);
        queryWrapper.like(ObjectUtil.isNotEmpty(TELPHONE), Equipment::getTELPHONE, TELPHONE);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpStatus), Equipment::getEqpStatus, eqpStatus);
        queryWrapper.like(ObjectUtil.isNotEmpty(CNCSYSTEM), Equipment::getCNCSYSTEM, CNCSYSTEM);
        queryWrapper.eq(ObjectUtil.isNotNull(BUYDATE), Equipment::getBUYDATE, BUYDATE);
        queryWrapper.like(ObjectUtil.isNotEmpty(eqpNum), Equipment::getEqpNum, eqpNum);
        queryWrapper.like(ObjectUtil.isNotEmpty(stationCode), Equipment::getStationCode, stationCode);*/

        return queryWrapper;
    }

}
